package net.lahuobao.finance.controller.fundchart;
import java.net.URLDecoder;
import java.time.LocalDate;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;

import net.lahuobao.export.datasoruce.entity.FinanceAccountDayRecord;
import net.lahuobao.export.datasoruce.entity.FinanceDetail;
import net.lahuobao.export.util.ExportFileUtil;
import net.lahuobao.finance.dao.FundChartMapper;
import net.lahuobao.finance.dao.HouseHoldMapper;
import net.lahuobao.finance.entity.charts.FinanceAccountDayRecordEntity;
import net.lahuobao.finance.entity.charts.FinanceDetailEntity;
import net.lahuobao.finance.entity.household.HouseholdEntity;
import net.lahuobao.finance.service.household.HouseholdServiceI;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.jeecgframework.core.beanvalidator.BeanValidators;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import java.io.IOException;
import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;

/**   
 * @Title: Controller
 * @Description: 资金明细账
 * @author zhangdaihao
 * @date 2016-08-24 18:49:17
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/financeDetailController")
public class FinanceDetailController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(FinanceDetailController.class);
	@Autowired
	private Validator validator;
	@Autowired
	private ExportFileUtil exportFileUtil;
	@Autowired
	private SystemService systemService;
	@Autowired
	private FundChartMapper fundChartDao;


	/**
	 * 导出
	 * @throws IOException 
	 */
	@RequestMapping(params = "exportFile")
	public void exportFile(HttpServletResponse response,HttpServletRequest request) throws IOException{
		exportFileUtil.setFileName("资金明细账" , "xlsx");
		Map<String, String> dict = getDetailDict("serviceType");
		Map paramMap = new HashMap();
		String organizationName = request.getParameter("organizationName");
		String userName = request.getParameter("userName");
		String operateTime_begin = request.getParameter("operateTime_begin");
		String operateTime_end = request.getParameter("operateTime_end");
		String operateType = request.getParameter("operateType");
		String orderId = request.getParameter("orderId");
		String waybillNumber = request.getParameter("waybillNumber");
		paramMap.put("organizationName", organizationName);
		paramMap.put("waybillNumber", waybillNumber);
		paramMap.put("operateTime_begin", operateTime_begin);
		paramMap.put("operateTime_end", operateTime_end);
		paramMap.put("operateType", operateType);
		paramMap.put("orderId", orderId);
		paramMap.put("userName", userName);

		//分页情况
		int pageCount  = 1000;
		int begin =  0;
		paramMap.put("begin", begin);
		paramMap.put("pageCount", pageCount);
		
		exportFileUtil.initExport(response);

		int begin1=exportFileUtil.exportIOStream(response, queryDataForExport(paramMap, dict), pageCount, begin,0);

		while(begin1!=begin){
			begin = begin1;
			paramMap.put("begin", begin1);
			begin1=exportFileUtil.exportIOStream(response, queryDataForExport(paramMap, dict), pageCount, begin1,1);
		}
		exportFileUtil.finish(response);
	}

	private Map<String, String> getDetailDict(String dictCode) {
		Map<String, String> map = new LinkedHashMap();
		List<Map<String, Object>> list = fundChartDao.selectDictByDictCode(dictCode);
		try {
			for(Map<String, Object> m : list) {
				map.put(m.get("typeCode").toString(), m.get("typeName").toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	/**
	 * 导出
	 * @throws IOException 
	 */
	@RequestMapping(params = "exportFile1")
	public void exportFile1(HttpServletResponse response,HttpServletRequest request) throws IOException{

		exportFileUtil.setFileName("资金总账" , "xlsx");

		Map paramMap = new HashMap();
		String organizationName = request.getParameter("organizationName");
		String userName = request.getParameter("userName");
		String createTime_begin = request.getParameter("createTime_begin");
		String createTime_end = request.getParameter("createTime_end");
		paramMap.put("organizationName", organizationName);
		paramMap.put("userName", userName);
		paramMap.put("createTime_begin", createTime_begin);
		paramMap.put("createTime_end", createTime_end);
		//分页情况
		int pageCount  = 1000;
		int begin =  0;
		paramMap.put("begin", begin);
		paramMap.put("pageCount", pageCount);
		
		exportFileUtil.initExport(response);

		int begin1=exportFileUtil.exportIOStream(response, queryDataForExport1(paramMap), pageCount, begin,0);

		while(begin1!=begin){
			begin = begin1;
			paramMap.put("begin", begin1);
			begin1=exportFileUtil.exportIOStream(response, queryDataForExport1(paramMap), pageCount, begin1,1);
		}
		exportFileUtil.finish(response);
	}


	/***
	 * 分页查询
	 */
	private List queryDataForExport(Map map, Map<String, String> dict){
		List<FinanceDetail> dataList = fundChartDao.selectFinanceDetailForExport(map);
		for(FinanceDetail detail : dataList) {
			if(dict.containsKey(detail.getOperateType())) {
				detail.setOperateType(dict.get(detail.getOperateType()));
			}
		}
		return dataList;
	}
	/***
	 * 分页查询
	 */
	private List queryDataForExport1(Map map){
		List<FinanceAccountDayRecord> dataList = fundChartDao.selectFinanceAccoutDayRecordForExport(map);
		return dataList;
	}
	/**
	 * 资金明细 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		String organizationName = request.getParameter("organizationName");
		String createTime_begin = request.getParameter("createTime_begin");
		String createTime_end = request.getParameter("createTime_end");

		request.setAttribute("organizationName", organizationName);
		request.setAttribute("createTime_begin", createTime_begin);
		request.setAttribute("createTime_end", createTime_end);
		return new ModelAndView("net/lahuobao/finance/charts/financeDetailList");
	}

	/**
	 * 资金总账列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list1")
	public ModelAndView list1(HttpServletRequest request) {
		request.setAttribute("stime", LocalDate.now().minusDays(1).toString());
		request.setAttribute("etime", LocalDate.now().toString());
		return new ModelAndView("net/lahuobao/finance/charts/financeAccountDayRecordList");
	}
	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(FinanceDetailEntity financeDetail,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(FinanceDetailEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, financeDetail, request.getParameterMap());
		
		if(null!=request.getSession().getAttribute("accountId")){
			Integer accountId =Integer.parseInt(request.getSession().getAttribute("accountId").toString());
			cq.eq("accountId", accountId);
			cq.add();
		
		}
		this.systemService.getDataGridReturn(cq, true);
		
		dataGrid.setFooter("userName:合计：,beforeAmount,plusamount,payamount,afterAmount");
		TagUtil.datagrid(response, dataGrid);
		

	}
	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */

	@RequestMapping(params = "datagrid1")
	public void datagrid1(FinanceAccountDayRecordEntity financeAccountDayRecord,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
//		CriteriaQuery cq = new CriteriaQuery(FinanceAccountDayRecordEntity.class, dataGrid);
//		//查询条件组装器
//		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, financeAccountDayRecord, request.getParameterMap());
//		this.systemService.getDataGridReturn(cq, true);
//		TagUtil.datagrid(response, dataGrid);
		Map paramMap = new HashMap();
		String createTime_begin = request.getParameter("createTime_begin");
		String createTime_end = request.getParameter("createTime_end");
		String organizationName = request.getParameter("organizationName");
		String userName = request.getParameter("userName");
		paramMap.put("createTime_begin", createTime_begin);
		paramMap.put("createTime_end", createTime_end);
		paramMap.put("organizationName", organizationName);
		paramMap.put("userName", userName);

		List<Map> list = fundChartDao.selectFinanceAccoutDayRecord(paramMap, systemService.getPageBounds());
        TagUtil.datagrid(response, pageDateGrid(list, dataGrid));

	}


}
